﻿//========================================================= 
/**@file ComplexCmdInstaller.h
 * @brief 命令装载器
 * 
 * @date 2018-07-28   10:14:52
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_COMPLEXCMDINSTALLER_H_
#define _LIBZHOUYB_COMPLEXCMDINSTALLER_H_
//--------------------------------------------------------- 
#include "../CommandRunner.h"
#include "../CommandInstaller.h"
//--------------------------------------------------------- 
namespace zhou_yb {
namespace application {
namespace driver {
//--------------------------------------------------------- 
/// 命令装载器
class ComplexCmdInstaller
{
protected:
    /// 需要操作的执行器
    Ref<CommandRunner> _runner;
    /// 获取执行器默认的脚本模块
    CommandSetter _GetAutoCMD()
    {
        const char CMD_ITEM[] = "AutoCMD";
        CommandSetter cmdSetter;
        Ref<CommandCollection> collection = _runner->Item(CMD_ITEM);
        if(collection.IsNull())
        {
            Ref<CommandRunnerCmdEditor<ArgParser> > refEditor;
            cmdSetter = _runner->New<CommandRunnerCmdEditor<ArgParser> >(CMD_ITEM, &refEditor);
            refEditor->Set(_runner);
        }
        else
        {
            cmdSetter.Set(*collection);
        }
        return cmdSetter;
    }
    /// 绑定命令
    size_t _BindCMD(Ref<CommandScriptActuator> devScript, const char* devId, const char* spaceName, const char* cmds)
    {
        CommandSetter cmdSetter = _GetAutoCMD();
        return CommandRunnerCmdEditor<ArgParser>::RebindCMD(cmdSetter, devScript, devId, spaceName, cmds);
    }
public:
    ComplexCmdInstaller() {}
    ComplexCmdInstaller(CommandRunner& runner) { Set(runner); }
    inline void Set(CommandRunner& runner)
    {
        _runner = runner;
    }
    /// 获取指定模块的脚本编辑器
    Ref<CommandScriptActuator> NewScriptCMD(const char* devId, string* pFullName = NULL)
    {
        if(_runner.IsNull())
            return Ref<CommandScriptActuator>();
        return CommandInstaller::NewScriptCMD(_runner, devId, pFullName);
    }
    /**
     * @brief 将模块命令绑定到根集合上
     * @date 2017-10-20 14:31
     * 
     * @param [in] devId 需要绑定的模块名称
     * @param [in] spaceName 新的命名空间名称
     * @param [in] cmds 需要绑定的命令名称,每个命令之间用';'分隔
     * @param [out] pCount [default:NULL] 实际绑定的命令个数
     * 
     * @return Ref<CommandScriptActuator> 模块的脚本执行器
     */
    Ref<CommandScriptActuator> BindCMD(const char* devId, const char* spaceName, const char* cmds)
    {
        Ref<CommandScriptActuator> devScript;
        if(_runner.IsNull() || _is_empty_or_null(devId) || _is_empty_or_null(cmds))
            return devScript;
        Ref<CommandCollection> devCollection = _runner->Item(devId);
        if(devCollection.IsNull())
            return devScript;

        devScript = NewScriptCMD(devId);
        _BindCMD(devScript, devId, spaceName, cmds);
        return devScript;
    }
};
//--------------------------------------------------------- 
} // nemespace driver
} // namespace application
} // namespace zhou_yb
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_COMPLEXCMDINSTALLER_H_
//========================================================= 